/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 */
package au.com.gworks.jump.app.wiki.client.navigation;

import org.javaongems.client.Gem;
import org.javaongems.client.forms.Form;
import org.javaongems.client.stdctrls.Image;
import org.javaongems.client.rt.text.DateFormatUtils;
import org.javaongems.client.winctrls.ListView;
import org.javaongems.client.winctrls.ListViewColumnHeaderFormatter;
import org.javaongems.client.winctrls.ListViewDataSource;
import org.javaongems.client.winctrls.ListViewDetailViewStyle;


import au.com.gworks.jump.app.wiki.client.service.WikiRpc;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;

public class MyspacesForm extends Form {
	private DockPanel outerPanel = new DockPanel();
	private TabPanel viewSwitcher = new TabPanel();
	private HorizontalPanel iconPanel = new HorizontalPanel();
	private HorizontalPanel detailPanel = new HorizontalPanel();
	private ListView listView = new ListView();
	private MySpacesStyler styler = new MySpacesStyler();

	private MyspacesDataSource			dataSource = new MyspacesDataSource();
	private MyspacesController 			controller;
	private WikiRpc.MyspacesInfo 	ctrlrModel;
	
	public MyspacesForm(MyspacesController ctrlr) {
		super(ctrlr);
		controller = ctrlr;
		initialiseLayout();
		initialiseWidgets();
		initialiseListeners();
	}

	private void initialiseLayout() {
		this.setCaption("Myspaces");
		add(outerPanel);
		viewSwitcher.add(iconPanel, " icon ");
		viewSwitcher.add(detailPanel, " detail ");
		iconPanel.add(listView);
		outerPanel.add(viewSwitcher, DockPanel.CENTER);
		setCloseFormButtonDisplayed(false);
		DOM.setAttribute(getElement(), "id", "jumpMyspacesWindow");
	}

	private void initialiseWidgets() {
		String imgPfx = Gem.stone.getApplImageCtx();
		com.google.gwt.user.client.ui.Image icon = new Image(imgPfx + "myspaces-x16.png");
		setFormIcon(icon);
		listView.setDataSource(dataSource);
		listView.setDetailViewStyle(styler);
		viewSwitcher.selectTab(0);
	}

	private void initialiseListeners() {
		setFormActionListener(controller);
		listView.setViewListener(controller);
		viewSwitcher.addTabListener(controller);
	}
	
	public void show() {
		super.show();
		paint();
	}

	public void refresh() {
		super.refresh();
		paint();
	}
	
	private void paint() {
		listView.render();
		resizeListView();
	}
	
	public void resizeListView() {
		int width = getStatusBarWidth();
		int lbTop = listView.getAbsoluteTop();
		int sbTop = getStatusBarTop();
		int height = sbTop - lbTop;
		listView.onResize(width, height);
	}
	
	public void setMyspacesModel(WikiRpc.MyspacesInfo model) {
		super.setModel(model);
		ctrlrModel = model;
	}
	
	/*pkg*/ void switchToTab(int tabIndex) {
		if (tabIndex == 0) {
			detailPanel.remove(listView);
			iconPanel.add(listView);
		} else {
			iconPanel.remove(listView);
			detailPanel.add(listView);
		}
		listView.setViewMode(tabIndex==1);
		paint();
	}

	private class MyspacesDataSource implements ListViewDataSource {
		public String getColumnDisplayName(int col) {
			switch (col) {
			case 0: return "Space";
			case 1: return "Date modified";
			case 2: return "Author";
			case 3: return "Revision";
			}
			return null;
		}

		public int getColCount() { return 4; }
		public int getDataRowCount() { return ctrlrModel == null ? 0 : ctrlrModel.items.length; }

		public String getDisplayCellData(int row, int col) {
			switch (col) {
			case 0: return ctrlrModel.items[row].namespace;
			case 1: return DateFormatUtils.formatAsIsoDate(ctrlrModel.items[row].lastModified);
			case 2: return ctrlrModel.items[row].lastUpdater;
			case 3: return ctrlrModel.items[row].revision.toString();
			}
			return null;
		}

		public Comparable getComparableCellData(int row, int col) {
			switch (col) {
			case 0: return ctrlrModel.items[row].namespace.toLowerCase();
			case 1: return new Long(ctrlrModel.items[row].lastModified);
			case 2: return ctrlrModel.items[row].lastUpdater.toLowerCase();
			case 3: return ctrlrModel.items[row].revision;
			}
			return null;
		}

		public String getDetailViewImageUrl(int row) {
			return Gem.stone.getApplImageCtx() + "repository-x16.png";
		}

		public String getIconViewImageUrl(int row) {
			return Gem.stone.getApplImageCtx() + "repository-x64.png";
		}
	}
	
	private class MySpacesStyler implements ListViewDetailViewStyle {
		public void styleHeader(int col, ListViewColumnHeaderFormatter frmtr) {
			HorizontalAlignmentConstant h = (col == 3 ? HasHorizontalAlignment.ALIGN_RIGHT: HasHorizontalAlignment.ALIGN_LEFT);
			frmtr.setAlignment(col, h, HasVerticalAlignment.ALIGN_MIDDLE);
		}

		public void styleRow(int[] sortedDataSrcMap, int[] dataSrcSortedMap, int gridRow, RowFormatter frmtr) {
		}

		public void styleCell(int[] sortedDataSrcMap, int[] dataSrcSortedMap, int gridRow, int gridCol, CellFormatter frmtr) {
			HorizontalAlignmentConstant h = (gridCol == 3 ? HasHorizontalAlignment.ALIGN_RIGHT: HasHorizontalAlignment.ALIGN_LEFT);
			frmtr.setAlignment(gridRow, gridCol, h, HasVerticalAlignment.ALIGN_MIDDLE);
		}
	}
}